package pers.cz.chat;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @version 1.0
 * @description:
 * @author: ChengZhi 0336
 * @create: 2025-01-23 14:46
 **/
public class Notification {

	private static final int DURATION = 3000; // 消息显示的时间（毫秒）
	private static final int OFFSET_X = 10;   // 距离屏幕右边的距离
	private static final int OFFSET_Y = 50;   // 距离屏幕底部的距离

	public static void showNotification(String message) {
		// 获取屏幕尺寸
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int screenWidth = screenSize.width;
		int screenHeight = screenSize.height;

		// 创建 JWindow 作为通知窗口
		JWindow notificationWindow = new JWindow();
		notificationWindow.setAlwaysOnTop(true); // 确保通知总是在最上方

		// 创建面板并设置布局
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		panel.setBackground(Color.decode("#F2F2F2")); // 设置背景颜色

		// 添加标签来显示消息
		JLabel label = new JLabel(message, SwingConstants.CENTER);
		label.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		label.setForeground(Color.BLACK);
		panel.add(label, BorderLayout.CENTER);

		// 设置窗口位置和大小
		notificationWindow.getContentPane().add(panel);
		notificationWindow.setSize(300, 80);
		notificationWindow.setLocation(screenWidth - notificationWindow.getWidth() - OFFSET_X,
				screenHeight - notificationWindow.getHeight() - OFFSET_Y);

		// 显示窗口
		notificationWindow.setVisible(true);

		// 设置定时器，在指定时间后关闭窗口
		Timer timer = new Timer(DURATION, new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				notificationWindow.dispose(); // 关闭窗口
			}
		});
		timer.setRepeats(false);
		timer.start();
	}
}
